/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MODEL.DAO;
import MODEL.POJO.*;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;


public class TestResultsDao {
    
    public static List<Testresults> LayDanhSachKetQua(){
        List<Testresults> ds = null; 
        //Session session = HibernateUtil.getSessionFactory().openSession();
        Session session = HibernateUtil.getSessionFactory().openSession();
        try { 
            String hql = "select kq from Testresults kq"; 
            Query query = session.createQuery(hql); 
            ds = query.list(); 
        } catch (HibernateException ex) { 
            System.err.println(ex); 
        } finally { 
           //session.close(); 
        } 
        return ds; 
    }
    public static int DemSoLuongBaiThiTheoDiem(float diem, int iduser)
    {
        Session session = HibernateUtil.getSessionFactory().openSession();
        int count = 0 ;
        List<Testresults> ds =null;
          try {
             // from Testresults t where t.testResultScore=4 and t.users =1
              //from Testresults t join t.users u where t.testResultScore = 4 and u.userId = 1
              //from Testresults t where t.testResultScore = 4 and t.users.userId = 1
        String HQL = "from Testresults t where t.testResultScore = :diem and t.users.userId = :iduser";             
        Query query = session.createQuery(HQL); 
        query.setParameter("diem", diem);
        query.setParameter("iduser", iduser);
        ds = query.list();
        count = ds.size();
        } catch (HibernateException ex) { 
            System.err.println(ex); 
        } finally { 
           session.close(); 
        } 
        
        return count;
    }
}
